package com.wxs.shiro.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wxs.common.pojo.RoleGroup;
import com.wxs.common.pojo.User;
import com.wxs.common.service.RoleGroupService;
import com.wxs.common.service.UserService;
import com.wxs.common.utils.DataGridModel;
import com.wxs.common.utils.DataResult;
import com.wxs.common.utils.JsonResult;
import com.wxs.common.utils.StringUtil;

/**
 * 用户管理
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;

	@Autowired
	private RoleGroupService roleGroupService;

	@RequestMapping("main.jhtml")
	public ModelAndView main() {
		return new ModelAndView("user/list");
	}

	/**
	 * 管理员列表数据获取,返回json
	 * 
	 * @param name
	 * @param enabled
	 * @param dgm
	 * @return
	 */
	@ResponseBody
	@RequestMapping("list.jhtml")
	public DataResult list(String name, Boolean enabled, DataGridModel dgm) {
		return userService.queryPage(name, enabled, dgm);
	}

	/**
	 * 管理员编辑界面
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("add.jhtml")
	public ModelAndView addPage() throws IOException {
		ModelAndView mv = new ModelAndView("user/add");
		List<RoleGroup> roleGroups = roleGroupService.listAll();
		mv.addObject("roleGroups", roleGroups);
		return mv;
	}

	/**
	 * 保存管理员到数据库
	 * 
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveAdd.jhtml", produces = "text/html;charset=UTF-8")
	public String saveAdd(User user) {
		try {
			if (userService.saveAdd(user) > 0) {
				return JsonResult.toSuccess(null, "保存成功！");
			} else {
				return JsonResult.toError(null, "保存失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "保存失败！");
		}
	}

	@RequestMapping("/all")
	@ResponseBody
	public Object findAllUser(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
		System.out.println("=====");
		return userService.findAllUser(pageNum, pageSize);
	}

	@RequestMapping("test")
	public ModelAndView test() {
		ModelAndView modelAndView = new ModelAndView("hello");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "Andy");
		modelAndView.addAllObjects(map);
		return modelAndView;
	}

	/**
	 * 检查用户名是否存在，存在返回true，否则false
	 * 
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doCheckName")
	public Boolean checkName(@RequestParam(value = "name", required = true) String name, Integer id) {
		return userService.checkName(name, id);
	}

	/**
	 * 管理员编辑页面
	 * 
	 * @param id
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("edit.jhtml")
	public ModelAndView editPage(@RequestParam(value = "id", required = true) Integer id) throws IOException {
		ModelAndView mv = new ModelAndView("user/edit");

		User user = userService.findById(id);
		mv.addObject("user", user);
		List<RoleGroup> roleGroups = roleGroupService.listAll();
		mv.addObject("roleGroups", roleGroups);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveEdit.jhtml", produces = "text/html;charset=UTF-8")
	public String saveEdit(User user) {
		try {
			User userold = userService.selectByName(user.getName());
			if (userold == null) {
				return JsonResult.toError(null, "修改失败,用户不存在！");
			}
			if (!userold.getPassword().equals(user.getPassword())) {
				user.setPassword(StringUtil.md5(user.getPassword()));
			}
			if (userService.saveEdit(user) > 0) {
				return JsonResult.toSuccess(null, "修改成功！");
			} else {
				return JsonResult.toError(null, "修改失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "修改失败！");
		}
	}

	/**
	 * 删除管理员
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "doDeleteUser", produces = "text/html;charset=UTF-8")
	public String doDeleteUser(@RequestParam(value = "id", required = true) Integer id) throws Exception {
		User user = userService.findById(id);
		if ("admin".equals(user.getName())) {
			return JsonResult.toError(null, "admin用户不允许删除！");
		} else {
			userService.deleteById(id);
			return JsonResult.toSuccess(null, "删除成功！");
		}
	}
}
